<?php
require_once APPLICATION_PATH . '/../library/4px/OrderOnline.php';
require_once APPLICATION_PATH . '/../library/4px/OrderOnlineTools.php';
class Uuch_4pxAPI {
    public static function CreatedBill($id,$agentNo,$forecastWeight,$forecastvolumeWeight)
    {
        header ( "content-type:text/html;charset=utf-8" );
        $billItem = Application_Model_wayBillItem::load($id);
        if(!$billItem){
            return array("type"=>0);
        }
        $invoiceItem = Application_Model_invoicedetail::bybillNo($billItem->billNo);
        if(!$invoiceItem){
            return array("type"=>2);
        }
        $weight = number_format($forecastWeight,2);
        if($forecastvolumeWeight != ""){
            if ($billItem->ExpressBy->isVolume == 1) {
                if ($forecastWeight < $forecastvolumeWeight) {
                    $weight = number_format($forecastvolumeWeight,2);
                }
            }
        }
                                
        $invoiceArr = array();
        foreach ($invoiceItem as $invoiceVal) {
            $ename = explode("（", $invoiceVal->Description);
            $ename = explode("(", $invoiceVal->Description);
            $invoiceArr[] = array(
                "declareNote" => "",//配货备注
                "declarePieces" => $invoiceVal->Num,//件数(默认: 1)
                "declareUnitCode" =>'PCE',//申报单位类型代码(默认:  PCE)，参照申报单位类型代码表
                "eName" => $ename[0],//海关申报英文品名
                "name" =>'',//海关申报中文品名
                "unitPrice" => $invoiceVal->Price,//单价 0 < Amount <= [10,2]【***】
            );
        }
        
        $soap = new OrderOnline();
        $arrs = array(
		"buyerId" => '',//买家ID
		"cargoCode" => 'P',//货物类型(默认：P)，参照货物类型表
		"city" => "",//城市 【***】
		"consigneeCompanyName" => $billItem->consignee,//收件人公司名称
		"consigneeEmail" => 'service@uuch.com',//收件人Email 
		"consigneeFax" => $billItem->conTel,//收件人传真号码
		"consigneeName" => $billItem->consignee,//收件人公司名称姓名【***】
		"consigneePostCode" => $billItem->conPostCode,//收件人邮编
		"consigneeTelephone" => $billItem->conTel,//收件人电话号码
		"customerWeight" => $weight,//客户自己称的重量(单位：KG) 
		"destinationCountryCode" => $billItem->CityBy->Word,//目的国家二字代码，参照国家代码表
		"initialCountryCode" => 'CN',//起运国家二字代码，参照国家代码表【***】
		"insurType" => '',//保险类型，参照保险类型表
		"insurValue" => '',//保险价值(单位：USD)0 < Amount <= [10,2] 
		"orderNo" => $billItem->BillNo,//客户订单号码，由客户自己定义【***】
		"orderNote" => '',//订单备注信息 
		"paymentCode" => 'P',//付款类型(默认：P)，参照付款类型表
		"pieces" => '1',//货物件数(默认：1) 0 < Amount <= [10,2] 
		"productCode" => $billItem->Channelname->Key,//产品代码，指DHL、新加坡小包挂号、联邮通挂号等，参照产品代码表 【***】
		"returnSign" => 'N',//小包退件标识 Y: 发件人要求退回 N: 无须退回(默认) 
		"shipperAddress" => "No.3 Langshan 2nd Street,Area of Hi-Tech Industrial Zone,Nanshan District, SHENZHEN",//发件人地址 
		"shipperCompanyName" => "Eyuki Commerce",//发件人公司名称 
		"shipperFax" => "075588605979",//发件人传真号码 
		"shipperName" => "Eyuki Commerce",//发件人姓名 
		"shipperPostCode" => "",//发件人邮编
		"shipperTelephone" => "075588605979",//发件人电话号码
		"stateOrProvince" => '',//州  /  省 【***】
		"street" => $billItem->conAdd,//街道【***】
		"trackingNumber" => '',//服务跟踪号码【无效时系统自动分配】
		"transactionId" => '',//交易ID 
		"declareInvoice" => $invoiceArr
        );
        $result = $soap->createAndPreAlertOrderService($arrs);
//        var_dump($arrs);
//        var_dump($result);exit;
        if($result["ack"] == "Success"){
            return array("type"=>1,"agentNo" =>$result["trackingNumber"]);
        } else {
            return array("type"=>0,"message"=>$result["cnMessage"]);
        }
    }
    public static function CreatedBillByWayId($id,$channer)
    {
        header ( "content-type:text/html;charset=utf-8" );
        $billItem = Application_Model_wayBillItem::load($id);
        if(!$billItem){
            return array("type"=>0);
        }
        $invoiceItem = Application_Model_invoicedetail::bybillNo($billItem->billNo);
        if(!$invoiceItem){
            return array("type"=>2);
        }
        $productWeight = $billItem->getWeightByType(Application_Model_WeightType::ProductWeight);
        $weight = number_format(($productWeight/1000),2);
        $invoiceArr = array();
        foreach ($invoiceItem as $invoiceVal) {
            $ename = explode("（", $invoiceVal->Description);
            $ename = explode("(", $invoiceVal->Description);
            $invoiceArr[] = array(
                "declareNote" => "",//配货备注
                "declarePieces" => $invoiceVal->Num,//件数(默认: 1)
                "declareUnitCode" =>'PCE',//申报单位类型代码(默认:  PCE)，参照申报单位类型代码表
                "eName" => $ename[0],//海关申报英文品名
                "name" =>'',//海关申报中文品名
                "unitPrice" => $invoiceVal->Price,//单价 0 < Amount <= [10,2]【***】
            );
        }
        $channerItem = Application_Model_ExpressChannel::load($channer);
        $soap = new OrderOnline();
        $arrs = array(
		"buyerId" => '',//买家ID
		"cargoCode" => 'P',//货物类型(默认：P)，参照货物类型表
		"city" => "",//城市 【***】
		"consigneeCompanyName" => $billItem->consignee,//收件人公司名称
		"consigneeEmail" => 'service@uuch.com',//收件人Email 
		"consigneeFax" => $billItem->conTel,//收件人传真号码
		"consigneeName" => $billItem->consignee,//收件人公司名称姓名【***】
		"consigneePostCode" => $billItem->conPostCode,//收件人邮编
		"consigneeTelephone" => $billItem->conTel,//收件人电话号码
		"customerWeight" => $weight,//客户自己称的重量(单位：KG) 
		"destinationCountryCode" => $billItem->CityBy->Word,//目的国家二字代码，参照国家代码表
		"initialCountryCode" => 'CN',//起运国家二字代码，参照国家代码表【***】
		"insurType" => '',//保险类型，参照保险类型表
		"insurValue" => '',//保险价值(单位：USD)0 < Amount <= [10,2] 
		"orderNo" => $billItem->BillNo,//客户订单号码，由客户自己定义【***】
		"orderNote" => '',//订单备注信息 
		"paymentCode" => 'P',//付款类型(默认：P)，参照付款类型表
		"pieces" => '1',//货物件数(默认：1) 0 < Amount <= [10,2] 
		"productCode" => $channerItem?$channerItem->Key:"",//产品代码，指DHL、新加坡小包挂号、联邮通挂号等，参照产品代码表 【***】
		"returnSign" => 'N',//小包退件标识 Y: 发件人要求退回 N: 无须退回(默认) 
		"shipperAddress" => "No.3 Langshan 2nd Street,Area of Hi-Tech Industrial Zone,Nanshan District, SHENZHEN",//发件人地址 
		"shipperCompanyName" => "Eyuki Commerce",//发件人公司名称 
		"shipperFax" => "075588605979",//发件人传真号码 
		"shipperName" => "Eyuki Commerce",//发件人姓名 
		"shipperPostCode" => "",//发件人邮编
		"shipperTelephone" => "075588605979",//发件人电话号码
		"stateOrProvince" => '',//州  /  省 【***】
		"street" => $billItem->conAdd,//街道【***】
		"trackingNumber" => '',//服务跟踪号码【无效时系统自动分配】
		"transactionId" => '',//交易ID 
		"declareInvoice" => $invoiceArr
        );
        $result = $soap->createAndPreAlertOrderService($arrs);
//        var_dump($arrs);exit;
        if($result["ack"] == "Success"){
            return array("type"=>1,"agentNo" =>$result["trackingNumber"]);
        } else {
            return array("type"=>0,"message"=>$result["cnMessage"]);
        }
    }
    
    public static function byAgentNo($agentNo)
    {
        $client = new OrderOnlineTools();
        $response = $client->findLabelDataService($agentNo);
        return $response;
    }
        public static function ChargeCalculateServer($cityItem,$weight,$expressId)
    {

        $client = new OrderOnlineTools();
        $arr=[];
        if($expressId==1){
            $productCode=array("E5");
        }elseif ($expressId==2) {
           $productCode=array("A1");
        }
        foreach ($cityItem as $city) {                            
        $arrs=[
        "cargoCode" => 'P',
        "productCode" => $productCode,        
        'countryCode'=>$city->Word,
        'weight'=>$weight,
        ];
        $response = $client->chargeCalculateService($arrs);
        //print_r($response);exit;
        sleep(0.2);
        if($response["ack"] == "Success"){
            $row['city']=$city->Name;                     
            $row['expressId']=$expressId;
            $row['cityId']=$city->Id; 
            $row['firstWeight']=500;
            $row['subsequentWeight']=500;
            $row['firstPrice']=number_format($response['freightAmount']*1.3,0);            
            $arr[]=$row;
             }
        }
        return $arr;
    }

}
?>
